package org.imixs.manik.webstat.jpa;

import java.util.Calendar;

import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Holds the informations about the last scan of an access log file. The LogFile
 * holds a manyToOne relationship to the AccessLog entity.
 * 
 * @author rsoika
 * 
 */
@javax.persistence.Entity
public class LogFile implements java.io.Serializable {

	private static final long serialVersionUID = 1L;

	private long id;
	private AccessLog accessLog;

	private String name;

	private Calendar lastentry;
	private Calendar lastmodified;

	/**
	 * default constructor for JPA
	 */
	public LogFile() {

		super();

	}

	/**
	 * returns the unique identifier for the Entity.
	 * 
	 * @return universal id
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public long getId() {
		return id;
	}

	protected void setId(long aID) {
		id = aID;
	}

	// we do not use : cascade = CascadeType.PERSIST
	// because of unidirectional relationship
	@ManyToOne(fetch = FetchType.LAZY)
	public AccessLog getAccessLog() {
		return accessLog;
	}

	public void setAccessLog(AccessLog accessLog) {
		this.accessLog = accessLog;
	}

	/**
	 * The timestamp from the last Log Entry which was imported.
	 * 
	 * @return timestamp
	 */
	@Temporal(TemporalType.TIMESTAMP)
	public Calendar getLastentry() {
		// test if lastmodified is null
		if (lastmodified == null) {
			// create default time
			lastmodified = Calendar.getInstance();
			lastmodified.set(Calendar.YEAR, 1970);
			lastmodified.set(Calendar.MONTH, 0);
			lastmodified.set(Calendar.DAY_OF_MONTH, 1);
		}
		return lastentry;
	}

	public void setLastentry(Calendar time) {
		this.lastentry = time;
	}

	/**
	 * The last modified time stamp from the file at the last scan.
	 * 
	 * @return timestamp
	 */
	@Temporal(TemporalType.TIMESTAMP)
	public Calendar getLastmodified() {
		// test if lastmodified is null
		if (lastmodified == null) {
			// create default time
			lastmodified = Calendar.getInstance();
			lastmodified.set(Calendar.YEAR, 1970);
			lastmodified.set(Calendar.MONTH, 0);
			lastmodified.set(Calendar.DAY_OF_MONTH, 1);
		}
		return lastmodified;
	}

	public void setLastmodified(Calendar lastmodified) {

		this.lastmodified = lastmodified;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
